/*
 * Copyright (C) 2010-2012 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package app.transitalarm;

import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;

import android.preference.Preference.OnPreferenceChangeListener;

import android.util.Log;

import app.transitalarm.model.DataRetrievalException;
import app.transitalarm.model.Stop;

import app.transitalarm.provider.Tables.Alarms;

//XXX validate stop before dismissing dialog.
public final class StopFirstAlarmEditor extends AlarmEditor {
    public static final String TAG = "StopFirstAlarmEditor";

//    private static final String TAG = "StopFirstAlarmEditor";
    @Override
    protected void initVectorPreferences() {
        stationPref = findPreference("station"); //Alarms.STATION_NAME);

        final EditTextPreference stationTextPref = (EditTextPreference) stationPref;
        stationTextPref.setText(a.getStopCode());
        stationTextPref.setOnPreferenceChangeListener(new StationPrefListener());

        routePref = (ListPreference) findPreference(Alarms.ROUTE);
        routePref.setValue(a.getRouteCode());
        routePref.setOnPreferenceChangeListener(new RoutePrefListener());

        directionPref = (ListPreference) findPreference(Alarms.DIRECTION);
        directionPref.setValue(a.getDirection());
        directionPref.setOnPreferenceChangeListener(new DirectionPrefListener());
    }

    @Override
    protected void updateStation() {
        stationPref.setSummary(a.getStopName());
    }

    @Override
    protected void updateRoute() {
        routePref.setSummary(a.getRoute().getName());

        try {
            final Stop stop = agency.getStopByCode(a.getStopCode());

            final String[] routes = stop.getRouteNames();
            final String[] routeCodes = stop.getRouteCodes();

            routePref.setEntries(routes);
            routePref.setEntryValues(routeCodes);
        } catch (final DataRetrievalException e) {
            routePref.setEntries(new String[] {  });
            routePref.setEntryValues(new String[] {  });

            Log.w(TAG, "Problems obtaining data for stop:" + a.getStopCode(), e);
        }
    }

    // ------------------ START Listeners

    //-- Vector Pref Listeners
    private final class StationPrefListener implements OnPreferenceChangeListener {
        @Override
        public boolean onPreferenceChange(final Preference preference, final Object newValue) {
            final String stopCode = newValue.toString().trim();

            Stop stop = null;

            try {
                stop = agency.getStopByCode(stopCode);
            } catch (final DataRetrievalException e) {
                Log.w(TAG, "Problems obtaining data for stop:" + stopCode, e);
            }

            if (stop != null) {
                a.setStop(stopCode);
                updateStation();
                a.setRoute("");
                updateRoute();
                a.setDirection("");
                updateDirection();

                return true;
            } else {
                Android.showLongToast(StopFirstAlarmEditor.this, R.string.could_not_locate_stop_please_retry, stopCode);

                return false;
            }
        }
    }

    private final class RoutePrefListener implements OnPreferenceChangeListener {
        @Override
        public boolean onPreferenceChange(final Preference preference, final Object newValue) {
            a.setRoute(newValue.toString());
            updateRoute();

            a.setDirection("");
            updateDirection();

            return true;
        }
    }

    private final class DirectionPrefListener implements OnPreferenceChangeListener {
        @Override
        public boolean onPreferenceChange(final Preference preference, final Object newValue) {
            a.setDirection(newValue.toString());
            updateDirection();

            return true;
        }
    }

    // ------------------ END Listeners
}
